import Vue from "vue";
import VueCookie from "vue-cookie";
import Router from "vue-router";

Vue.use(Router)

//解决编程式路由往同一地址跳转时会报错的情况
const originalPush = Router.prototype.push;
const originalReplace = Router.prototype.replace;
//push
Router.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject)
        return originalPush.call(this, location, onResolve, onReject);
    return originalPush.call(this, location).catch(err => err);
};
//replace
Router.prototype.replace = function push(location, onResolve, onReject) {
    if (onResolve || onReject)
        return originalReplace.call(this, location, onResolve, onReject);
    return originalReplace.call(this, location).catch(err => err);
};

//提供一个重置路由的方法
export const restRouter = () => {
    router.matcher = new Router({
        mode: 'history',
        routes
    })
}

const routes = [
    {
        path: '/',
        name: 'manage',
        redirect: '/home',
        component: resolve => require(['../pages/manage.vue'], resolve),
        meta: {
            title: '首页'
        },
        children: [
/*            {
                path: '/home',
                name: 'home',
                component: resolve => require(['../pages/home.vue'], resolve),
                meta: {
                    title: '首页'
                },
            },*/
            /*{
                path: 'system/user',
                name: 'user',
                component: resolve => require(['../pages/system/user.vue'], resolve),
                meta: {
                    title: '用户管理'
                },
            },
            {
                path: 'system/role',
                name: 'role',
                component: resolve => require(['../pages/system/role.vue'], resolve),
                meta: {
                    title: '角色管理'
                },
            },
            {
                path: 'system/menu',
                name: 'menu',
                component: resolve => require(['../pages/system/menu.vue'], resolve),
                meta: {
                    title: '菜单管理'
                },
            },

            {
                path: '/content/category',
                name: 'category',
                component: resolve => require(['../pages/system/category.vue'], resolve),
                meta: {
                    title: '分类管理'
                },
            },
            {
                path: '/content/product',
                name: 'product',
                component: resolve => require(['../pages/system/product.vue'], resolve),
                meta: {
                    title: '商品管理'
                },
            },
            {
                path: '/content/order',
                name: 'order',
                component: resolve => require(['../pages/system/order.vue'], resolve),
                meta: {
                    title: '订单管理'
                },
            },*/
        ]
    },

    {
        path: '/login',
        name: 'login',
        component: resolve => require(['../pages/login.vue'], resolve)
        // component: () => import('@/pages/login.vue')
    }
]

const router = new Router({
    mode: 'history',
    routes
})

//路由前置守卫
router.beforeEach((to, from, next) => {
    //判断token不存在的情况
    const token = VueCookie.get('satoken')
    //token不存在，说明当前用户是未登录状态，需要跳转至登录页面
    if (!token && to.name !== 'login') {
        next({
            name: 'login'
        })
    } else if (token && to.name === 'login') { //token存在，不允许用户再跳转回登录页面
        next({
            name: 'home'
        })
    } else {
        next()
    }
})

export default router
